//
//  ThirdViewController.m
//  IPhoneX适配
//
//  Created by 训网高 on 2018/4/28.
//  Copyright © 2018年 训网高. All rights reserved.
//

#import "ThirdViewController.h"

@interface ThirdViewController () <UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *unReadArray;
@property (nonatomic, strong) NSMutableArray *readArray;
@property (nonatomic, strong) NSMutableArray *sectionArray;
@end

@implementation ThirdViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor cyanColor];
    self.title = @"出行助手";
    
    self.unReadArray = [NSMutableArray array];
    self.readArray = [NSMutableArray array];
    self.sectionArray = [NSMutableArray arrayWithObject:@"0"];
    [self creatTableView];
    [self creatDataSource];
}
- (void)creatTableView {
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    [self.view addSubview:self.tableView];
}

- (void)creatDataSource {
    for (int i = 0; i < 20; i++) {
        if (i < 15) {
            [self.readArray addObject:[NSString stringWithFormat:@"%d",i]];
        }
        [self.unReadArray addObject:[NSString stringWithFormat:@"%d",i]];
    }
    [self.tableView reloadData];
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    NSString *sectionString = [NSString stringWithFormat:@"%ld",section];
    if ([self.sectionArray containsObject:sectionString]) {
        if (section == 0) {
            return self.unReadArray.count;
        } else {
            return self.readArray.count;
        }
    } else {
        return 0;
    }
//    if (section == 0) {
//        return 20;
//    } else {
//        return 15;
//    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 40;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 40;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *headerView = [[UIView alloc] init];
    
    UILabel *textLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 40)];
    textLabel.userInteractionEnabled = YES;
    if (section == 0) {
        textLabel.text = [NSString stringWithFormat:@"未读消息        （%ld）",self.unReadArray.count];
    } else {
        textLabel.text = [NSString stringWithFormat:@"历史消息        （%ld）",self.readArray.count];;
    }
    
    [headerView addSubview:textLabel];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(0, 0, SCREEN_WIDTH, 40);
    [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    button.tag = 1000+section;
    [textLabel addSubview:button];
    
    return headerView;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *identifier = @"cell";
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    if (indexPath.section == 0) {
        cell.textLabel.text = [NSString stringWithFormat:@"%@",self.unReadArray[indexPath.row]];
    } else {
        cell.textLabel.text = [NSString stringWithFormat:@"%@",self.readArray[indexPath.row]];
    }
    return cell;
}

#pragma mark ---- 按钮点击事件
- (void)buttonClick:(UIButton *)sender {
    NSInteger index = sender.tag - 1000;
    NSString *indexString = [NSString stringWithFormat:@"%ld",index];
    if ([self.sectionArray containsObject:indexString]) {
        [self.sectionArray removeObject:indexString];
    } else {
        [self.sectionArray addObject:indexString];
    }
    [self.tableView reloadData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
